package com.zxk.blog.controller;

import com.zxk.blog.feign.CrawlerEsFeignService;
import com.zxk.blog.service.EsSearchDataService;
import com.zxk.core.common.util.JsonData;
import com.zxk.core.common.util.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(value = "爬虫系统对外提供的数据查询接口")
@RestController
@RequestMapping("/blog/api/es")
public class EsDataProviderController {

    @Autowired
    private EsSearchDataService esSearchDataService;

    @Resource
    private CrawlerEsFeignService crawlerEsFeignService;


    @ApiOperation(value="es数据查询",notes="es数据查询")
    @PostMapping("/get/{index}/{field}/{keyword}/{pageNo}/{pageSize}")
    public JsonData getEsDada( @PathVariable(value = "index") String index,
                               @PathVariable(value = "field") String field,
                               @PathVariable(value = "keyword") String keyword,
                               @PathVariable(value = "pageNo") Integer pageNo,
                               @PathVariable(value = "pageSize") Integer pageSize){
        List<Map<String, Object>> mapList = esSearchDataService.searchData(field, keyword, pageSize, pageNo, index);
        return JsonData.buildSuccess(mapList,200);
    }


    @ApiOperation(value = "es新闻数据查询")
    @PostMapping("/search/news")
    public R searchNews(@RequestParam String date,@RequestParam(required = false) String[] fields) {
        Map<String,Object> condition = new HashMap<>();
        condition.put("date",date);
        condition.put("fields",fields);
        R response = crawlerEsFeignService.searchNewsByCondition(condition);
        return response;
    }

}

